package com.frank.loadinglibrary.common;

import com.frank.loadinglibrary.utils.Indicator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

import java.util.ArrayList;

public class LineScaleComponent extends BaseLoadingComponent implements Component.DrawTask {

    private Indicator indicator;
    public static final float SCALE = 1.0f;
    float[] scaleYFloats = new float[]{SCALE,
            SCALE,
            SCALE,
            SCALE,
            SCALE,};
    private Paint mPaint;

    public LineScaleComponent(Context context) {
        this(context, null);
    }

    public LineScaleComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }

    public LineScaleComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init();
    }

    public LineScaleComponent(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
        init();

    }

    private void init() {
        setPaint();
        setDurtion(900);
        indicator = new Indicator<AnimatorValue>() {
            @Override
            public ArrayList onCreateAnimators() {
                return LineScaleComponent.this.onCreateAnimators();
            }
        };
        setBindStateChangedListener(new BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                if (autoStart) {
                    startAnimators();
                }
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {
                stopAnimators();
            }
        });
        addDrawTask(this);
    }

    public ArrayList<AnimatorValue> onCreateAnimators() {
        ArrayList<AnimatorValue> animators = new ArrayList<>();
        long[] delays = new long[]{500, 250, 0, 250, 500};
        for (int i = 0; i < 5; i++) {
            final int index = i;
            AnimatorValue scaleAnim = new AnimatorValue();
            scaleAnim.setDuration(durtion);
            scaleAnim.setLoopedCount(-1);
            scaleAnim.setDelay(delays[i]);
            indicator.addUpdateListener(scaleAnim, (animatorValue, v) -> {
                if (v < 0.5f)
                    scaleYFloats[index] = (float) (-1.4 * v + 1);
                else
                    scaleYFloats[index] = (float) (1.4 * v - 0.4);
                invalidate();
            });
            animators.add(scaleAnim);
        }
        return animators;

    }

    @Override
    protected void stopAnimators() {
        if (indicator != null && indicator.isRunning())
            indicator.stop();
    }

    @Override
    protected void startAnimators() {
        if (indicator != null && !indicator.isRunning())
            indicator.start();
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        float translateX = getWidth() / 11;
        float translateY = getHeight() / 2;
        for (int i = 0; i < 5; i++) {
            canvas.save();
            canvas.translate((2 + i * 2) * translateX - translateX / 2, translateY);
            canvas.scale(SCALE, scaleYFloats[i]);
            RectFloat rectF = new RectFloat(-translateX / 2, -getHeight() / 2.5f, translateX / 2, getHeight() / 2.5f);
            canvas.drawRoundRect(rectF, 5, 5, mPaint);
            canvas.restore();
        }
    }

    public void setPaint() {
        if (mPaint == null) {
            mPaint = new Paint();
            mPaint.setColor(Color.WHITE);
            mPaint.setStyle(Paint.Style.FILL_STYLE);
            mPaint.setAntiAlias(true);
        }
    }
}
